#!/bin/env Rscript


library(mHMM)

# data(control_vec)
# data(sample_vec)
# data(posi_vec)

argss <- commandArgs(trailingOnly = TRUE)

print(argss)

pos <- argss[2]
samp <- argss[3]
ctrl <- argss[4]
out_prefix0 <- argss[5]
out_prefix1 <- argss[6]
corr=T
if (argss[7]=='F') {
	corr =F
}


sample_vec <- read.table(samp)
control_vec <- read.table(ctrl)
posi_vec <- read.table(pos)

sample_vec <- as.matrix(sample_vec, mode="numeric")
sample_vec <- as.vector(sample_vec, mode="numeric")
control_vec <- as.matrix(control_vec, mode="numeric")
control_vec <- as.vector(control_vec, mode="numeric")
posi_vec <- as.matrix(posi_vec, mode="numeric")
posi_vec <- as.vector(posi_vec, mode="numeric")


# len <- length(posi_vec)

# len <- len %/% 10

# control_vec <- control_vec[1:len]
# sample_vec <- sample_vec[1:len]
# posi_vec <- posi_vec[1:len]

reference_reads_input <- control_vec
sample_reads_input <- sample_vec
loci_input <- posi_vec
kmeans1 <- TRUE
C.const <- 50
# change_point_refine <- TRUE



CNV_detection_result <- m_HMM_main(sample_reads_input, reference_reads_input, loci_input, kmeans1=kmeans1, C.const=C.const, change_point_refine = corr)





filename <- paste(out_prefix0, out_prefix1, "txt", sep=".")

# "CNV_detection_before_refine.RData"
# "CNV_detection_refine.RData"       
# "combined_result.RData"

write.table(CNV_detection_result, filename, quote = F, sep = "\t", row.names = F, col.names = F)

q()